﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Threading;
using AForge.Imaging.Filters;
using AForge;

namespace BulkImageSplitter
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private void browseInputButton_Click(object sender, EventArgs e)
        {
            var folderDialog = new FolderBrowserDialog();
            folderDialog.Description = "Select the input directory";
            var result = folderDialog.ShowDialog();

            if (result == System.Windows.Forms.DialogResult.OK)
            {
                inputDirectoryTextBox.Text = folderDialog.SelectedPath;
            }
        }

        private void browseOutputButton_Click(object sender, EventArgs e)
        {
            var folderDialog = new FolderBrowserDialog();
            folderDialog.Description = "Select the output directory";
            var result = folderDialog.ShowDialog();

            if (result == System.Windows.Forms.DialogResult.OK)
            {
                outputDirectoryTextBox.Text = folderDialog.SelectedPath;
            }
        }

        private void processButton_Click(object sender, EventArgs e)
        {
            if (!Directory.Exists(inputDirectoryTextBox.Text))
            {
                MessageBox.Show(this, "Invalid input directory.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (!Directory.Exists(outputDirectoryTextBox.Text))
            {
                MessageBox.Show(this, "Invalid output directory.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            ColorFiltering backgroundFilter = new ColorFiltering();

            if (whiteBackgroundButton.Checked)
            {
                backgroundFilter.Red = new IntRange(240, 255);
                backgroundFilter.Green = new IntRange(240, 255);
                backgroundFilter.Blue = new IntRange(240, 255);
            }
            else
            {
                backgroundFilter.Red = new IntRange(0, 32);
                backgroundFilter.Green = new IntRange(0, 32);
                backgroundFilter.Blue = new IntRange(0, 32);
            }

            processButton.Enabled = false;

            BulkSplitter bulkSplitter = new BulkSplitter(LogMessage, EnableProcessButton);
            bulkSplitter.InputDirectory = inputDirectoryTextBox.Text;
            bulkSplitter.OutputDirectory = outputDirectoryTextBox.Text;
            bulkSplitter.BackgroundFilter = backgroundFilter;
            bulkSplitter.MinimumSize = new Size((int)minimumWidth.Value, (int)minimumHeight.Value);

            Thread processingThread = new Thread(new ThreadStart(bulkSplitter.Process));
            processingThread.Start();
        }

        private void LogMessage(string message)
        {
            if (InvokeRequired)
            {
                Action<string> method = LogMessage;
                Invoke(method, message);
                return;
            }

            logTextBox.Text += message + "\r\n";
        }

        private void EnableProcessButton()
        {
            if (InvokeRequired)
            {
                Action method = EnableProcessButton;
                Invoke(method);
                return;
            }

            processButton.Enabled = true;
        }
    }
}
